home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 6 / CU Amiga Magazine's Super CD-ROM 06 (1996)(EMAP Images)(GB)(Track 1 of 4)[!][issue 1997-01].iso / cucd / prog / dopussdk / examples / envoymodule / envoy.c < prev    next >
C/C++ Source or Header  |  1996-09-05  |  12KB  |  558 lines

  1. #include "envoy.h"
  2. #include <clib/exec_protos.h>
  3. #include <clib/dos_protos.h>
  4. #include <pragmas/exec_pragmas.h>
  5. #include <pragmas/dos_pragmas.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8.  
  9. // Define the version string here
  10. static char *version="$VER: envoy.module 1.0 (01.08.96)";
  11.  
  12. // Main entry point to the module
  13. int __asm __saveds L_Module_Entry(
  14.     register __a0 char *args,                // User-supplied arguments
  15.     register __a1 struct Screen *screen,    // Screen to open on
  16.     register __a2 IPCData *ipc,                // Our IPC pointer
  17.     register __a3 IPCData *main_ipc,        // Main Opus IPC pointer
  18.     register __d0 ULONG mod_id,                // ID of module function
  19.     register __d1 EXT_FUNC(func_callback))    // Opus callback function
  20. {
  21.     struct Window *window=0;
  22.     NewConfigWindow newwin;
  23.     ObjectList *objlist;
  24.     struct Library *AccountsBase;
  25.     struct function_entry *entry;
  26.     BPTR lock,dirlock,olddir;
  27.     struct FileInfoBlock __aligned fib;
  28.     struct UserInfo *user_info;
  29.     struct GroupInfo *group_info=0;
  30.     APTR path_node;
  31.     char path[256];
  32.  
  33.     // Get first selected entry from Opus
  34.     if (!(entry=(struct function_entry *)func_callback(EXTCMD_GET_ENTRY,IPCDATA(ipc),0)))
  35.         return 0;
  36.  
  37.     // Get source path
  38.     path_node=(APTR)func_callback(EXTCMD_GET_SOURCE,IPCDATA(ipc),path);
  39.  
  40.     // Open accounts.library
  41.     if (!(AccountsBase=OpenLibrary("accounts.library",0)))
  42.         return 0;
  43.  
  44.     // Try and get window to open over
  45.     if (!(newwin.nw_Parent=(void *)func_callback(EXTCMD_GET_WINDOW,IPCDATA(ipc),path_node)))
  46.     {
  47.         // If no window, use screen
  48.         newwin.nw_Parent=screen;
  49.         newwin.nw_Flags=WINDOW_SCREEN_PARENT;
  50.     }
  51.     else newwin.nw_Flags=0;
  52.  
  53.     // Initialise rest of structure
  54.     newwin.nw_Dims=&_envoy_window;
  55.     newwin.nw_Title=DOpusGetString(locale,MSG_ENVOY_TITLE);
  56.     newwin.nw_Locale=locale;
  57.     newwin.nw_Flags|=WINDOW_REQ_FILL|WINDOW_AUTO_KEYS|WINDOW_VISITOR;
  58.     newwin.nw_Port=0;
  59.     newwin.nw_Font=0;
  60.  
  61.     // Allocate some data and open window
  62.     if ((user_info=AllocUserInfo()) &&
  63.         (group_info=AllocGroupInfo()) &&
  64.         (window=OpenConfigWindow(&newwin)) &&
  65.         (objlist=AddObjectList(window,_envoy_objects)))
  66.     {
  67.         UWORD owner_id=0,group_id=0;
  68.         short break_flag=0;
  69.  
  70.         // Lock path
  71.         if (dirlock=Lock(path,ACCESS_READ))
  72.         {
  73.             // Change directory
  74.             olddir=CurrentDir(dirlock);
  75.  
  76.             // Lock first file
  77.             if (lock=Lock(entry->name,ACCESS_READ))
  78.             {
  79.                 short a;
  80.  
  81.                 // Examine file
  82.                 Examine(lock,&fib);
  83.                 UnLock(lock);
  84.  
  85.                 // Does file have an owner?
  86.                 if (fib.fib_OwnerUID && !(IDToUser(fib.fib_OwnerUID,user_info)))
  87.                 {
  88.                     // Fill out owner field
  89.                     SetGadgetValue(objlist,GAD_ENVOY_OWNER_FIELD,(ULONG)user_info->ui_UserName);
  90.                 }
  91.  
  92.                 // Does file have a group?
  93.                 if (fib.fib_OwnerGID && !(IDToGroup(fib.fib_OwnerGID,group_info)))
  94.                 {
  95.                     // Fill out group field
  96.                     SetGadgetValue(objlist,GAD_ENVOY_GROUP_FIELD,(ULONG)group_info->gi_GroupName);
  97.                 }
  98.  
  99.                 // Go through protection bits
  100.                 for (a=0;a<4;a++)
  101.                 {
  102.                     // Group bit set?
  103.                     if (fib.fib_Protection&(1<<(FIBB_GRP_READ-a)))
  104.                         SetGadgetValue(objlist,GAD_ENVOY_GROUP_READ+a,1);
  105.  
  106.                     // Other bit set?
  107.                     if (fib.fib_Protection&(1<<(FIBB_OTR_READ-a)))
  108.                         SetGadgetValue(objlist,GAD_ENVOY_OTHER_READ+a,1);
  109.                 }
  110.             }
  111.  
  112.             // Loop for messages
  113.             FOREVER
  114.             {
  115.                 struct IntuiMessage *msg;
  116.                 IPCMessage *imsg;
  117.  
  118.                 // IPC message?
  119.                 while (imsg=(IPCMessage *)GetMsg(ipc->command_port))
  120.                 {
  121.                     // Quit?
  122.                     if (imsg->command==IPC_QUIT ||
  123.                         imsg->command==IPC_HIDE ||
  124.                         imsg->command==IPC_ABORT) break_flag=1;
  125.                     IPC_Reply(imsg);
  126.                 }
  127.  
  128.                 // Any intuition messages?
  129.                 while (msg=GetWindowMsg(window->UserPort))
  130.                 {
  131.                     struct IntuiMessage msg_copy;
  132.  
  133.                     // Copy message and reply
  134.                     msg_copy=*msg;
  135.                     ReplyWindowMsg(msg);
  136.  
  137.                     // Look at message
  138.                     switch (msg_copy.Class)
  139.                     {
  140.                         // Gadget
  141.                         case IDCMP_GADGETUP:
  142.  
  143.                             // Look at gadget ID
  144.                             switch (((struct Gadget *)msg_copy.IAddress)->GadgetID)
  145.                             {
  146.                                 // Owner list
  147.                                 case GAD_ENVOY_OWNER:
  148.  
  149.                                     // Put up owner list
  150.                                     envoy_owner_list(window,objlist,user_info,AccountsBase);
  151.                                     break;
  152.  
  153.                                 // Group list
  154.                                 case GAD_ENVOY_GROUP:
  155.  
  156.                                     // Put up group list
  157.                                     envoy_group_list(window,objlist,group_info,AccountsBase);
  158.                                     break;
  159.  
  160.                                 // Okay
  161.                                 case GAD_ENVOY_OK:
  162.  
  163.                                     // Check valid owner
  164.                                     if (!(envoy_check_owner(
  165.                                         ipc,
  166.                                         window,
  167.                                         objlist,
  168.                                         user_info,
  169.                                         AccountsBase,
  170.                                         &owner_id)) ||
  171.  
  172.                                     // Check valid group
  173.                                         !(envoy_check_group(
  174.                                         ipc,
  175.                                         window,
  176.                                         objlist,
  177.                                         group_info,
  178.                                         AccountsBase,
  179.                                         &group_id))) break;
  180.  
  181.                                     // Set break flag
  182.                                     break_flag=2;
  183.                                     break;
  184.  
  185.                                 // Cancel
  186.                                 case GAD_ENVOY_CANCEL:
  187.                                     break_flag=1;
  188.                                     break;
  189.                             }
  190.                             break;
  191.  
  192.  
  193.                         // Close window
  194.                         case IDCMP_CLOSEWINDOW:
  195.                             break_flag=1;
  196.                             break;
  197.  
  198.  
  199.                         // Key press
  200.                         case IDCMP_RAWKEY:
  201.  
  202.                             // Help?
  203.                             if (msg_copy.Code==0x5f && !(QualValid(msg_copy.Qualifier)))
  204.                             {
  205.                                 // Set busy pointer
  206.                                 SetWindowBusy(window);
  207.  
  208.                                 // Send help request
  209.                                 func_callback(EXTCMD_GET_HELP,IPCDATA(ipc),"NetSet");
  210.  
  211.                                 // Clear busy pointer
  212.                                 ClearWindowBusy(window);
  213.                             }
  214.                             break;
  215.                     }
  216.                 }
  217.  
  218.                 // Look at break flag
  219.                 if (break_flag) break;
  220.  
  221.                 // Wait for messages
  222.                 Wait(    1<<window->UserPort->mp_SigBit|
  223.                         ((ipc)?(1<<ipc->command_port->mp_SigBit):0));
  224.             }
  225.  
  226.             // Want to change settings?
  227.             if (break_flag==2)
  228.             {
  229.                 ULONG protect=0;
  230.                 ULONG owner_info;
  231.                 short a;
  232.                 APTR progress;
  233.                 long count=0;
  234.  
  235.                 // Look at protection gadgets
  236.                 for (a=0;a<4;a++)
  237.                 {
  238.                     // Group bit set?
  239.                     if (GetGadgetValue(objlist,GAD_ENVOY_GROUP_READ+a))
  240.                         protect|=(1<<(FIBB_GRP_READ-a));
  241.  
  242.                     // Other bit set?
  243.                     if (GetGadgetValue(objlist,GAD_ENVOY_OTHER_READ+a))
  244.                         protect|=(1<<(FIBB_OTR_READ-a));
  245.                 }
  246.  
  247.                 // Close window
  248.                 CloseConfigWindow(window);
  249.                 window=0;
  250.  
  251.                 // Build owner info longword
  252.                 owner_info=(owner_id<<16)|group_id;
  253.  
  254.                 // Open progress indicator
  255.                 progress=
  256.                     OpenProgressWindowTags(
  257.                         PW_Window,func_callback(EXTCMD_GET_WINDOW,IPCDATA(ipc),path_node),
  258.                         PW_Title,DOpusGetString(locale,MSG_ENVOY_PROGRESS_TITLE),
  259.                         PW_FileCount,func_callback(EXTCMD_ENTRY_COUNT,IPCDATA(ipc),0),
  260.                         PW_Flags,    PWF_FILENAME|
  261.                                     PWF_GRAPH|
  262.                                     PWF_ABORT,
  263.                         TAG_END);
  264.  
  265.                 // Go through entries
  266.                 while (entry=(struct function_entry *)func_callback(EXTCMD_GET_ENTRY,IPCDATA(ipc),0))
  267.                 {
  268.                     struct endentry_packet packet;
  269.  
  270.                     // Update progress info
  271.                     SetProgressWindowTags(progress,
  272.                         PW_FileName,entry->name,
  273.                         PW_FileNum,++count,
  274.                         TAG_END);
  275.  
  276.                     // Check abort
  277.                     if ((func_callback(EXTCMD_CHECK_ABORT,IPCDATA(ipc),0)) ||
  278.                         CheckProgressAbort(progress))
  279.                         break;
  280.  
  281.                     // Try to lock entry
  282.                     if (lock=Lock(entry->name,ACCESS_READ))
  283.                     {
  284.                         // Examine file
  285.                         Examine(lock,&fib);
  286.                         UnLock(lock);
  287.  
  288.                         // Mask out existing network bits from protection
  289.                         fib.fib_Protection&=~(FIBF_GRP_READ|FIBF_GRP_WRITE|FIBF_GRP_EXECUTE|FIBF_GRP_DELETE|FIBF_OTR_READ|FIBF_OTR_WRITE|FIBF_OTR_EXECUTE|FIBF_OTR_DELETE);
  290.  
  291.                         // Or in new protection bits
  292.                         fib.fib_Protection|=protect;
  293.  
  294.                         // Set protection bits
  295.                         SetProtection(entry->name,fib.fib_Protection);
  296.                     }
  297.  
  298.                     // Set owner, fill out packet with results
  299.                     packet.entry=entry;
  300.                     packet.deselect=SetOwner(entry->name,owner_info);
  301.  
  302.                     // Mark function for reload (if successful)
  303.                     if (packet.deselect)
  304.                         func_callback(EXTCMD_RELOAD_ENTRY,IPCDATA(ipc),entry);
  305.  
  306.                     // End this entry
  307.                     func_callback(EXTCMD_END_ENTRY,IPCDATA(ipc),&packet);
  308.                 }
  309.  
  310.                 // Turn progress indicator off
  311.                 CloseProgressWindow(progress);
  312.             }
  313.  
  314.             // Restore directory
  315.             UnLock(CurrentDir(olddir));
  316.         }
  317.     }
  318.  
  319.     // Close down
  320.     CloseConfigWindow(window);
  321.     CloseLibrary(AccountsBase);
  322.     FreeGroupInfo(group_info);
  323.     FreeUserInfo(user_info);
  324.     return 1;
  325. }
  326.  
  327.  
  328. // Display owner list
  329. void envoy_owner_list(
  330.     struct Window *window,
  331.     ObjectList *objlist,
  332.     struct UserInfo *owner_info,
  333.     struct Library *AccountsBase)
  334. {
  335.     Att_List *owner_list;
  336.     Att_Node *node;
  337.     short sel;
  338.     char *ptr;
  339.  
  340.     // Create list
  341.     if (!(owner_list=Att_NewList(0)))
  342.         return;
  343.  
  344.     // Make window busy
  345.     SetWindowBusy(window);
  346.  
  347.     // Go through owners
  348.     owner_info->ui_UserID=0;
  349.     while ((NextUser(owner_info))==0)
  350.     {
  351.         // Add this owner to the list
  352.         Att_NewNode(owner_list,owner_info->ui_UserName,1,ADDNODEF_SORT);
  353.     }
  354.  
  355.     // Add "no owner" entry
  356.     node=Att_NewNode(owner_list,DOpusGetString(locale,MSG_ENVOY_NO_OWNER),0,ADDNODEF_SORT);
  357.  
  358.     // Get current selection
  359.     ptr=(char *)GetGadgetValue(objlist,GAD_ENVOY_OWNER_FIELD);
  360.     if (!ptr || !*ptr) sel=Att_FindNodeNumber(owner_list,node);
  361.     else sel=Att_NodeNumber(owner_list,ptr);
  362.  
  363.     // Display owner list
  364.     if ((sel=SelectionList(
  365.         owner_list,
  366.         window,
  367.         0,
  368.         DOpusGetString(locale,MSG_ENVOY_GET_OWNER),
  369.         sel,
  370.         0,
  371.         0,
  372.         DOpusGetString(locale,MSG_OK),
  373.         DOpusGetString(locale,MSG_CANCEL)))!=-1)
  374.     {
  375.         // Get selected node
  376.         if (node=Att_FindNode(owner_list,sel))
  377.         {
  378.             // Fill out owner field
  379.             SetGadgetValue(
  380.                 objlist,
  381.                 GAD_ENVOY_OWNER_FIELD,
  382.                 (node->data)?(ULONG)node->node.ln_Name:0);
  383.         }
  384.     }
  385.  
  386.     // Clear window busy
  387.     ClearWindowBusy(window);
  388.  
  389.     // Free list
  390.     Att_RemList(owner_list,0);
  391. }
  392.  
  393.  
  394. // Display group list
  395. void envoy_group_list(
  396.     struct Window *window,
  397.     ObjectList *objlist,
  398.     struct GroupInfo *group_info,
  399.     struct Library *AccountsBase)
  400. {
  401.     Att_List *group_list;
  402.     Att_Node *node;
  403.     short sel;
  404.     char *ptr;
  405.  
  406.     // Create list and GroupInfo structure
  407.     if (!(group_list=Att_NewList(0)))
  408.         return;
  409.  
  410.     // Make window busy
  411.     SetWindowBusy(window);
  412.  
  413.     // Go through groups
  414.     group_info->gi_GroupID=0;
  415.     while ((NextGroup(group_info))==0)
  416.     {
  417.         // Add this group to the list
  418.         Att_NewNode(group_list,group_info->gi_GroupName,1,ADDNODEF_SORT);
  419.     }
  420.  
  421.     // Add "no group" entry
  422.     node=Att_NewNode(group_list,DOpusGetString(locale,MSG_ENVOY_NO_GROUP),0,ADDNODEF_SORT);
  423.  
  424.     // Get current selection
  425.     ptr=(char *)GetGadgetValue(objlist,GAD_ENVOY_GROUP_FIELD);
  426.     if (!ptr || !*ptr) sel=Att_FindNodeNumber(group_list,node);
  427.     else sel=Att_NodeNumber(group_list,ptr);
  428.  
  429.     // Display group list
  430.     if ((sel=SelectionList(
  431.         group_list,
  432.         window,
  433.         0,
  434.         DOpusGetString(locale,MSG_ENVOY_GET_GROUP),
  435.         sel,
  436.         0,
  437.         0,
  438.         DOpusGetString(locale,MSG_OK),
  439.         DOpusGetString(locale,MSG_CANCEL)))!=-1)
  440.     {
  441.         // Get selected node
  442.         if (node=Att_FindNode(group_list,sel))
  443.         {
  444.             // Fill out group field
  445.             SetGadgetValue(
  446.                 objlist,
  447.                 GAD_ENVOY_GROUP_FIELD,
  448.                 (node->data)?(ULONG)node->node.ln_Name:0);
  449.         }
  450.     }
  451.  
  452.     // Clear window busy
  453.     ClearWindowBusy(window);
  454.  
  455.     // Free list and GroupInfo
  456.     Att_RemList(group_list,0);
  457. }
  458.  
  459.  
  460. // Check for valid owner
  461. BOOL envoy_check_owner(
  462.     IPCData *ipc,
  463.     struct Window *window,
  464.     ObjectList *objlist,
  465.     struct UserInfo *owner_info,
  466.     struct Library *AccountsBase,
  467.     UWORD *owner_id)
  468. {
  469.     char *ptr;
  470.     char buf[80];
  471.  
  472.     // Get pointer to owner name
  473.     ptr=(char *)GetGadgetValue(objlist,GAD_ENVOY_OWNER_FIELD);
  474.  
  475.     // No owner?
  476.     if (!ptr || !*ptr)
  477.     {
  478.         *owner_id=0;
  479.         return 1;
  480.     }
  481.  
  482.     // Check for valid user
  483.     if (!(NameToUser(ptr,owner_info)))
  484.     {
  485.         // Ok
  486.         *owner_id=owner_info->ui_UserID;
  487.         return 1;
  488.     }
  489.  
  490.     // Build requester text
  491.     sprintf(buf,DOpusGetString(locale,MSG_ENVOY_BAD_USER),ptr);
  492.  
  493.     // Display error
  494.     AsyncRequestTags(
  495.         ipc,
  496.         REQTYPE_SIMPLE,
  497.         window,
  498.         0,
  499.         0,
  500.         AR_Message,buf,
  501.         AR_Title,DOpusGetString(locale,MSG_ENVOY_TITLE),
  502.         AR_Button,DOpusGetString(locale,MSG_OK),
  503.         TAG_END);
  504.  
  505.     // Invalid
  506.     return 0;
  507. }
  508.  
  509.  
  510. // Check for valid group
  511. BOOL envoy_check_group(
  512.     IPCData *ipc,
  513.     struct Window *window,
  514.     ObjectList *objlist,
  515.     struct GroupInfo *group_info,
  516.     struct Library *AccountsBase,
  517.     UWORD *group_id)
  518. {
  519.     char *ptr;
  520.     char buf[80];
  521.  
  522.     // Get pointer to group name
  523.     ptr=(char *)GetGadgetValue(objlist,GAD_ENVOY_GROUP_FIELD);
  524.  
  525.     // No group?
  526.     if (!ptr || !*ptr)
  527.     {
  528.         *group_id=0;
  529.         return 1;
  530.     }
  531.  
  532.     // Check for valid group
  533.     if (!(NameToGroup(ptr,group_info)))
  534.     {
  535.         // Ok
  536.         *group_id=group_info->gi_GroupID;
  537.         return 1;
  538.     }
  539.  
  540.     // Build requester text
  541.     sprintf(buf,DOpusGetString(locale,MSG_ENVOY_BAD_GROUP),ptr);
  542.  
  543.     // Display error
  544.     AsyncRequestTags(
  545.         ipc,
  546.         REQTYPE_SIMPLE,
  547.         window,
  548.         0,
  549.         0,
  550.         AR_Message,buf,
  551.         AR_Title,DOpusGetString(locale,MSG_ENVOY_TITLE),
  552.         AR_Button,DOpusGetString(locale,MSG_OK),
  553.         TAG_END);
  554.  
  555.     // Invalid
  556.     return 0;
  557. }
  558.